from typing import Optional, List, Dict
from pydantic import BaseModel


class ChatSessionBase(BaseModel):
    id :int 
    user_id: int
    title: str = "新对话"
    model: str = "qwen3:8b"


class ChatSessionCreate(ChatSessionBase):
    pass  # 继承自 ChatSessionBase，无需额外字段


class ChatSessionUpdate(BaseModel):
    id: int  # 👈 必须指定要更新哪个会话
    title: Optional[str] = None
    model: Optional[str] = None




class ChatMessageBase(BaseModel):
    role: str  # 'user' 或 'assistant'
    content: str


class ChatMessageCreate(ChatMessageBase):
    session_id: int


class ChatMessageUpdate(BaseModel):
    id: int
    content: Optional[str] = None






class ChatSessionResponse(BaseModel):
    """
    返回给前端的对话会话详情（GET /sessions/{id} 或 GET /sessions）
    """
    id: int
    user_id: int
    title: str
    model: str
    created_at: str  # ISO 8601 时间字符串
    updated_at: str
    is_deleted: bool = False


class ChatMessageResponse(BaseModel):
    """
    返回给前端的消息列表（GET /sessions/{session_id}/messages）
    """
    id: int
    session_id: int
    role: str  # 'user' or 'assistant'
    content: str
    created_at: str  # ISO 8601 时间字符串


class ChatResponse(BaseModel):
    """
    AI 回复的响应模型（POST /aichat）
    """
    response_text: str
    is_complete: bool = True  # 是否因长度截断
    usage: Optional[dict] = None  # token 使用情况：{prompt_tokens: 12, ...}
    timestamp: str  # ISO 8601 时间戳

#  AI 聊天请求模型
class ChatRequest(BaseModel):
    user_id: int = None
    user_message: str
    system_prompt: Optional[str] = "你是一个友好、专业的AI助手。"
    history: Optional[List[Dict[str, str]]] = None  # [{"role": "user", "content": "..."}, ...]
    max_tokens: Optional[int] = 1000
    temperature: Optional[float] = 0.7
    model:str = "qwen3:8b"
    session_id: int = None,  # 可选参数：恢复旧会话